package com.tbit.uqbike.webmanager.controller;

import com.alibaba.fastjson.JSON;
import com.tbit.common.constant.CommonConstant;
import com.tbit.common.constant.MethodEnum;
import com.tbit.common.constant.ModuleEnum;
import com.tbit.uqbike.annos.OperateLog;
import com.tbit.uqbike.manager.exception.BaseException;
import com.tbit.uqbike.object.business.NoticeSearch;
import com.tbit.uqbike.object.business.NoticeStatisticsSearch;
import com.tbit.uqbike.object.pojo.Notice;
import com.tbit.uqbike.object.pojo.NoticeAll;
import com.tbit.uqbike.object.pojo.NoticeStatistics;
import com.tbit.uqbike.object.pojo.dto.NoticeStatisticsDTO;
import com.tbit.uqbike.object.pojo.vo.NoticeVO;
import com.tbit.uqbike.object.view.JsonResult;
import com.tbit.uqbike.service.business.NoticeService;
import com.tbit.uqbike.webmanager.util.Assert;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Slf4j
@RestController
@RequestMapping("/notice")
public class NoticeController extends BaseController {

    @Autowired
    private NoticeService noticeService;



    /**
     * @description: 获取公告列表
     * @author: mo.shanyong
     * @date: 2025/2/19 16:33
     * @param: noticeSearch 搜索条件
     * @return: com.tbit.uqbike.object.view.JsonResult
     */
    @GetMapping("/getNoticeList")
    public JsonResult getNoticeList(NoticeSearch noticeSearch,HttpServletRequest request){
//        log.info("noticeSearch:{}", JSON.toJSONString(noticeSearch));
        try {
            List<NoticeAll> noticeAllList = noticeService.queryByPage(noticeSearch,getLogin(request));
            Integer count = noticeService.queryCount(noticeSearch);
            count = Objects.isNull(count) ? 0 : count;
            return JsonResult.ok(noticeAllList,count);
        }catch (BaseException e){
            log.info("e",e);
        }
        return JsonResult.ok(new ArrayList<>(),0);

    }

    /**
     * @description: 添加公告
     * @author: mo.shanyong
     * @date: 2025/2/19 16:53
     * @param: noticeVO
     * @return: com.tbit.uqbike.object.view.JsonResult
     */
    @PostMapping("/addNotice")
    @OperateLog(module = ModuleEnum.NOTICE, methods = MethodEnum.NOTICE_ADD)
    public JsonResult addNotice(@RequestBody NoticeVO noticeVO, HttpServletRequest request){
//        log.info("noticeVO:{}",JSON.toJSONString(noticeVO));
        Assert.notEmptyAndHasAppointLen(noticeVO.getPlatformList(),1,"指定生效平台不能为空");
        Assert.notEmptyAndHasAppointLen(noticeVO.getAccountIdList(),1,"生效区域不能为空");
        Assert.notEmptyAndHasAppointLen(noticeVO.getAppointUserTypeList(), Math.min(noticeVO.getPlatformList().size(), 2),"指定展示的人员类型和选择展示的平台的数量不符");
        noticeService.insertCheckNull(noticeVO,getLogin(request));
        return JsonResult.ok();
    }

    /**
     * @description: 编辑公告
     * @author: mo.shanyong
     * @date: 2025/2/19 17:01
     * @param: noticeVO
     * @return: com.tbit.uqbike.object.view.JsonResult
     */
    @OperateLog(module = ModuleEnum.NOTICE, methods = MethodEnum.NOTICE_EDIT)
    @PutMapping("/editNotice")
    public JsonResult editNotice(NoticeVO noticeVO){
//        log.info("editNotice------->noticeVO:{}",JSON.toJSONString(noticeVO));
        noticeService.updateCheckNull(noticeVO);
        return JsonResult.ok();
    }
    /**
     * @description: 删除公告
     * @author: mo.shanyong
     * @date: 2025/2/19 17:03
     * @param: id
     * @return: com.tbit.uqbike.object.view.JsonResult
     */
    @OperateLog(module = ModuleEnum.NOTICE, methods = MethodEnum.NOTICE_DEL)
    @PostMapping("/delNotice")
    public JsonResult delNotice(Integer id){
//        log.info("delNotice------------------->id:{}",id);
        noticeService.deleteById(id);
        return JsonResult.ok();
    }

    /**
     * @description: 获取当前用户的公告列表
     * @author: mo.shanyong
     * @date: 2025/2/19 17:12
     * @return: com.tbit.uqbike.object.view.JsonResult
     */
    @GetMapping("/getUserNoticeList")
    public JsonResult getUserNoticeList(Integer pageNum,Integer pageSize,HttpServletRequest request){
        pageSize = pageSize > 1000 ? 100 : pageSize;
        List<Notice> notices = noticeService.getUserNoticeList(pageNum, pageSize, getLogin(request), CommonConstant.NOTICE_MANAGER_PLATFORM);
        int noticeCount = noticeService.getUserNoticeCount(getLogin(request),CommonConstant.NOTICE_MANAGER_PLATFORM);
        int userNotReadNoticeCount = noticeService.getUserNotReadNoticeCount(CommonConstant.NOTICE_MANAGER_PLATFORM, getLogin(request));
        JsonResult ok = JsonResult.ok(notices, noticeCount);
        ok.setNum(userNotReadNoticeCount);
        return ok;
    }
    /**
     * @description: 获取公告
     * @author: mo.shanyong
     * @date: 2025/2/19 18:13
     * @param: id 公告id
     * @return: com.tbit.uqbike.object.view.JsonResult
     */
    @GetMapping("/getNotice")
    public JsonResult getNotice(Integer id){
        NoticeAll noticeAll = noticeService.queryById(id);
        return JsonResult.ok(noticeAll);
    }
    /**
     * @description: 记录对公告的操作日志
     * @author: mo.shanyong
     * @date: 2025/2/20 15:48
     * @param: operateType 操作类型：3、点击；5、阅读
     * @return: com.tbit.uqbike.object.view.JsonResult
     */
    @PostMapping("/recordNoticeLog")
    public JsonResult recordNoticeLog(Integer noticeId ,Integer operateType,HttpServletRequest request){
//        log.info("recordNoticeLog------->noticeId:{},operateType:{}",noticeId,operateType);
        Integer logId = noticeService.recordNoticeLog(noticeId, operateType, getLogin(request),CommonConstant.NOTICE_MANAGER_PLATFORM);
        return JsonResult.ok(logId);
    }


}
